package mongo.db.clients.firsts;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.QueryBuilder;
import com.mongodb.ServerAddress;

public class TimeSlice {

	/**
	 * @param args
	 */
	
	private static DB db;
	private static MongoClient client;
	static List<ServerAddress> seeds;
	
	
	public static void main(String[] args) throws UnknownHostException {
		
		seeds = new ArrayList<ServerAddress>();
		seeds.add(new ServerAddress("SS59-MAA", 27021));
		seeds.add(new ServerAddress("SS59-MAA", 27022));
		
		client = new MongoClient(seeds);
		
		db = client.getDB("netflow"); 
		
		Calendar testStart = Calendar.getInstance();
//		testStart.add(Calendar.HOUR_OF_DAY, -4);
		System.out.println("testStart : " + testStart.getTime());
		
		Calendar testStop = testStart.getInstance();
		System.out.println("testStop : " + testStop.getTime());
		
		System.exit(1);
		
		DBCollection flows = db.getCollection("timegen");
		
		
/*		SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");
		Calendar start = new GregorianCalendar();
		Calendar end = new GregorianCalendar();
		
//		end.set(Calendar.MINUTE, 0);
		end.set(Calendar.SECOND, 0);
		end.set(Calendar.MILLISECOND, 0);
		
//		start.set(Calendar.HOUR_OF_DAY, end.get(Calendar.HOUR_OF_DAY - 1));
		start.set(Calendar.MINUTE, end.get(Calendar.MINUTE - 3));
//		start.set(Calendar.MINUTE, 0);
		start.set(Calendar.SECOND, 0);
		start.set(Calendar.MILLISECOND, 0);
//		System.out.println(sdf.format(start.getTime()));
		
		System.out.println("start : " + sdf.format(start.getTime()) + " : end : " + sdf.format(end.getTime()));*/
		
//		now.setMinutes(30);
//		now.setSeconds(0);
		
//		Date before = new Date(now.getTime());
//		before.setMinutes(0);
//		before.setSeconds(0);
		
		
		Date now = new Date();
		Date before = new Date(now.getTime() - (1000 * 60 * 60));
		Date before10 = new Date(before.getTime() - (1000 * 60 * 10));
		
		
		System.out.println("now is : " + now);
		System.out.println("before is : " + before);
		System.out.println("before10 is : " + before);
		
		
		
		Calendar nowCal = Calendar.getInstance();
		System.out.println("now cal initialized : " + nowCal.getTime());
		nowCal.set(Calendar.MINUTE, 0);
		nowCal.set(Calendar.SECOND, 0);
		nowCal.set(Calendar.MILLISECOND, 0);
		
		Calendar beforeCal = nowCal.getInstance();
		System.out.println("before cal nowcal instalce : " + beforeCal.getTime());
		beforeCal.add(Calendar.HOUR_OF_DAY, -1);
		
		System.out.println("nowCal : " + nowCal.getTime());
		System.out.println("beforeCal : " + beforeCal.getTime());
		
//		System.exit(1);
		
		BasicDBObject query = new BasicDBObject("timestamp", new BasicDBObject(
				"$gt", "Tue Jan 21 11:44:31 IST 2014").append("$lt",
				"Tue Jan 21 11:44:34 IST 2014"));
		
		TimeZone tz = TimeZone.getTimeZone("GMT");
		Calendar start = Calendar.getInstance();
		start.set(Calendar.HOUR_OF_DAY, 18);
		start.set(Calendar.MINUTE, 34);
		start.set(Calendar.SECOND, 30);
		
		Calendar stop  = Calendar.getInstance();
		start.set(Calendar.HOUR_OF_DAY, 18);
		stop.set(Calendar.MINUTE, 34);
		stop.set(Calendar.SECOND, 40);
		
		System.out.println("start : " + start.getTime());
		System.out.println("stop : " + stop.getTime());
		System.out.println("before time zone : " + start.getTimeZone().getDisplayName());
		System.out.println("time before time zone : " + start.getTime());
//		start.setTimeZone(tz);
		System.out.println("after time zone : " + start.getTimeZone().getDisplayName());
		System.out.println("time after time zone : " + start.getTime());
		
		
		BasicDBObject query1 = new BasicDBObject("timestamp", new BasicDBObject("$gt", before).append("$lt", new BasicDBObject("$lt", now)));
		BasicDBObject query2 = new BasicDBObject("timestamp", new BasicDBObject("$lt", before));
		BasicDBObject query3 = new BasicDBObject("timestamp", new BasicDBObject("$gt", beforeCal.getTime()).append("$lt", new BasicDBObject("$lt", nowCal.getTime())));
		BasicDBObject query4 = new BasicDBObject("time", new BasicDBObject("$gt", start.getTime()).append("$lt", stop.getTime()));
		
		DBObject query5 = QueryBuilder.start().put("time").greaterThan(start.getTime()).get();
		
		DBCursor iter = flows.find(query4);
		System.out.println("fetched records : " + iter.count());
		
	}
// -Duser.timezone=GMT
}
